from collections import deque
from typing import *


class MinStack:

    def __init__(self):
        self.q = deque()
        self.mn = deque()

    def push(self, x: int) -> None:
        self.q.append(x)
        if len(self.mn) == 0 or x < self.mn[-1][1]:
            self.mn.append((len(self.q), x))

    def pop(self) -> None:
        self.q.pop()
        if self.mn[-1][0] > len(self.q):
            self.mn.pop()

    def top(self) -> int:
        return self.q[-1]

    def min(self) -> int:
        return self.mn[-1][1]
